const WebSocket = require('ws')

const server = new WebSocket.Server({ port: 8080 })

server.on('connection', (socket) => {
    console.log('成功建立连接')

    // 接收消息
    socket.on('message', (message) => {
        console.log(`Received message: ${message}`)
        // 发送
        socket.send(`Server received: ${message}`)
    })

    socket.on('close', () => {
        console.log('Client disconnected')
    })
})

console.log('WebSocket server is running on ws://localhost:8080')
